#include <vector>
#include <set>

class Solution
{
public:
	bool containsNearbyAlmostDuplicate(std::vector<int>& nums, int k, int t)
	{
		std::multiset<int> data;

		for (int i = 0; i < k; i++) {
			if (i<nums.size()) data.insert(nums[i]);
			else break;
		}
		for (int i = 0; i < nums.size(); i++) {
			if (i + k < nums.size()) data.insert(nums[i+k]);
			auto num = nums[i];
			data.erase(data.find(num));
			auto low = data.lower_bound(num - t);
			if (low != data.cend() && *low <= (num + t)) return true;
		}
		return false;
	}
};